package com.jp.fm.servlet;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easymock.EasyMock;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jp.fm.EasyMockParent;
import com.jp.fm.dao.AccountDao;
import com.jp.fm.dao.BillReminderDao;
import com.jp.fm.data.Account;
import com.jp.fm.data.BillReminder;

public class SaveRemindersTest extends EasyMockParent {

	private SaveReminders servlet = null;

	private HttpServletRequest mockRequest = null;
	private HttpServletResponse mockResponse = null;
	private RequestDispatcher mockRequestDispatcher = null;
	private AccountDao mockAccountDao = null;
	private BillReminderDao mockBillReminderDao = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		super.setUp();

		servlet = new SaveReminders();
	}

	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}

	@Test
	public final void testDoPost() throws ServletException, IOException {

		Vector<String> values = new Vector<String>();
		values.add("name_1");
		Enumeration<String> enumStrs = values.elements();

		Class[] args = { HttpServletRequest.class, HttpServletResponse.class };

		SaveReminders partialMockServlet = (SaveReminders) createPartialMock(
				SaveReminders.class, "doPost", args, false);

		partialMockServlet.setAccountDao(mockAccountDao);
		partialMockServlet.setBillReminderDao(mockBillReminderDao);

		expect(mockRequest.getParameterNames()).andReturn(enumStrs);
		expect(mockRequest.getParameter("name_1")).andReturn("name");
		expect(mockRequest.getParameter("id_1")).andReturn(null);
		expect(mockRequest.getParameter("amount_1")).andReturn("10.99");
		expect(mockRequest.getParameter("account_1")).andReturn("account");
		expect(mockRequest.getParameter("datepicker_1"))
				.andReturn("01/20/1977");
		expect(mockRequest.getParameter("repeat_1")).andReturn("Monthly");
		expect(mockAccountDao.findByName("account")).andReturn(
				new Account(null, null, null, null, null, null, 0));
		expect(mockBillReminderDao.writeObject(anyObject(BillReminder.class)))
				.andReturn(Integer.valueOf(1));
		partialMockServlet.doBillReminderDeletes(anyObject(List.class));
		EasyMock.expectLastCall();

		expect(mockRequest.getRequestDispatcher(anyObject(String.class)))
				.andReturn(mockRequestDispatcher);
		mockRequestDispatcher.forward(mockRequest, mockResponse);
		EasyMock.expectLastCall();

		replayMocks();
		EasyMock.replay(partialMockServlet);

		partialMockServlet.doPost(mockRequest, mockResponse);

		EasyMock.verify(partialMockServlet);
	}
}
